package com.fckbug.action.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.fckbug.service.UserService;
import com.fckbug.util.SpringContextHolder;
import com.fckbug.util.WebappUtil;
import com.fckbug.vos.Acc;
import com.mysql.jdbc.StringUtils;

/**
 * 查看用户的信息
 * @author think
 *
 */
public class ModifyProfileAction extends Action {
	UserService userService = (UserService)SpringContextHolder.getBean("userService");
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String nickName = request.getParameter("nickname");
		String email = request.getParameter("email");
		String realName = request.getParameter("realname");
		String company = request.getParameter("company");
		String location = request.getParameter("location");
		String birthday = request.getParameter("birthday");
		if(StringUtils.isNullOrEmpty(id) || StringUtils.isNullOrEmpty(nickName) || StringUtils.isNullOrEmpty(email)) {
			request.setAttribute("detail", "请输入昵称和Email");
			request.getRequestDispatcher("/error/detail.jsp").forward(request, response);
			return null;
		}
		int a = Integer.parseInt(id);
		Acc acc = WebappUtil.getLoginUser(request);
		if(!acc.getId().equals(a)) {
			request.setAttribute("detail", "小样，使坏啊");
			request.getRequestDispatcher("/error/detail.jsp").forward(request, response);
			return null;
		}
		acc = userService.getUserByUserId(Integer.parseInt(id));
		if(acc.getId()==null) {
			response.sendRedirect("/error/404.jsp");
			return null;
		}
		acc.setNickName(nickName);
		acc.setEmail(email);
		acc.setRealName(realName);
		acc.setCompany(company);
		acc.setLocation(location);
		acc.setBirthday(birthday);
		boolean success = userService.updateUser(acc);
		if(success) {
			request.setAttribute("acc", acc);
			response.sendRedirect("/user/profile.do?id="+id);
		} else {
			request.setAttribute("detail", "保存失败");
			request.getRequestDispatcher("/error/detail.jsp").forward(request, response);
		}
		return null;
	}
}
